﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;

namespace AttentenceCheckSystem
{
    /// <summary>
    /// Interaction logic for ApplyOvertimeWorkWindow.xaml
    /// </summary>
    public partial class ApplyOvertimeWorkWindow : Window
    {
        public ApplyOvertimeWorkWindow()
        {
            InitializeComponent();
            OverworkDate.SelectedDate = DateTime.Now;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (staffSelector.SelectedIndex == -1)
            {
                MessageBox.Show("请选择申请加班的员工");
                return;
            }

            try
            {
                string strConn = Properties.Settings.Default.Database1ConnectionString;

                using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn))
                {
                    conn.Open();
                    System.Data.OleDb.OleDbCommand command = conn.CreateCommand();
                    command.CommandText = "INSERT INTO 加班申请(uid, 日期) Values(@user, @date)";
                    command.Parameters.Add("@user", OleDbType.Integer).Value = staffSelector.SelectedValue;
                    command.Parameters.Add("@date", OleDbType.Date).Value = OverworkDate.SelectedDate;
                    if (command.ExecuteNonQuery() == 0)
                    {
                        MessageBox.Show("记录插入失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else
                    {
                        MessageBox.Show("记录插入成功", "信息", MessageBoxButton.OK, MessageBoxImage.Information);
                        HolidayGen holidayGen = new HolidayGen();
                        holidayGen.AddHoliday(Convert.ToInt32(staffSelector.SelectedValue),
                                              Convert.ToDateTime(OverworkDate.SelectedDate));

                        ObjectDataProvider obj = FindResource("OverworkView") as ObjectDataProvider;
                        if (obj != null)
                        {
                            obj.Refresh();
                        }
                    }
                }
            }
            catch (Exception ec)
            {
                MessageBox.Show(ec.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
